package com.tgy.jwt.config;

import com.tgy.jwt.interceptor.TokenAuthInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author tanggy
 * @date 2022/4/27 14:29
 */
@Configuration
public class RequestIntercepterConfig implements WebMvcConfigurer {

    @Value("${interceptor.path}")
    private String path;
    @Value("${interceptor.exclude-path}")
    private String excludePath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (StringUtils.isEmpty(path)){
            return;
        }
        TokenAuthInterceptor requestInterceptor = new TokenAuthInterceptor();
        InterceptorRegistration registration = registry.addInterceptor(requestInterceptor).addPathPatterns(path.split(","));

        if (!StringUtils.isEmpty(excludePath)) {
            registration.excludePathPatterns(excludePath.split(","));
        }
    }
}
